﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using org.alex.edss;

namespace testServer
{
    public enum OperationAction
    {
        Inited=0,
        StartHandShake=1,
        HandShake1=2,
        HandShake2=3,
        HandShake3=4,
        HandShakeSuccess=5,
        Action=6,
        PushingData=7,
        SendingData=8
    }
    class Program
    {
        static void Main(string[] args)
        {
            EdsServer server = new EdsServer(1935, 200, 1000, 4096);
            server.AppStart += new ApplicationEventHandler(server_AppStart);
            server.AppError += new ApplicationEventHandler(server_AppError);
            server.AppEnd += new ApplicationEventHandler(server_AppEnd);
            server.SessionStart += new SessionEventHandler(server_SessionStart);
            server.Start();
            Console.Read();

        }

        static void server_SessionStart(Session session, int ReadBytes)
        {
            //throw new NotImplementedException();
            Console.WriteLine("会话" + session.Key + "创建 at:" + Thread.CurrentThread.ManagedThreadId);
            session.OnReading += new SessionEventHandler(session_OnReading);
            session.ReadComplete += new SessionEventHandler(session_ReadComplete);
            session.SendComplete += new SessionEventHandler(session_SendComplete);
            session.SessionEnd += new SessionEventHandler(session_SessionEnd);
            session.SessionError += new SessionEventHandler(session_SessionError);
            session.UserData.Add("session", new SessionData());
            session.Connection.Read();
        }

        static void session_SessionError(Session session, int ReadBytes)
        {
            //throw new NotImplementedException();
            Console.WriteLine("会话" + session.Key + "出错 at:" + Thread.CurrentThread.ManagedThreadId);
        }

        static void session_SessionEnd(Session session, int ReadBytes)
        {
            //throw new NotImplementedException();
            Console.WriteLine("会话" + session.Key + "关闭 at:" + Thread.CurrentThread.ManagedThreadId);
        }

        static void session_SendComplete(Session session, int ReadBytes)
        {
            //throw new NotImplementedException();
            SessionData ses = (SessionData)session.UserData["session"];
            switch (ses.Action)
            {
                case OperationAction.StartHandShake:
                    ses.Action = OperationAction.HandShake1;
                    session.Connection.Read() ; return;
            }
         
        }

        static void session_ReadComplete(Session session, int ReadBytes)
        {
            //throw new NotImplementedException();
          
        }

        static void session_OnReading(Session session, int ReadBytes)
        {
            //throw new NotImplementedException();
            SessionData ses = (SessionData)session.UserData["session"];
            switch (ses.Action)
            {
                case OperationAction.Inited: FirstHandShake(session, ReadBytes, ses); return;
                case OperationAction.HandShake1: SecendHandShake(session, ReadBytes, ses); break;
            }
   
        }

        static void SecendHandShake(Session session, int Offset, SessionData SesData)
        {
            if (Offset > 0)
            {
                string pg1 = Encoding.UTF8.GetString(SesData.ShakeData.ToArray(), 1, 1536);
                string pg2 = Encoding.UTF8.GetString(session.ReadBuffer, 0, 1536);
                if (pg1 == pg2)
                {
                    SesData.Action = OperationAction.HandShakeSuccess;
                    MemoryStream tms = new MemoryStream();
                    tms.Write(session.ReadBuffer, 1536, Offset - 1537);
                    byte[] bf = tms.ToArray();
                    using (FileStream fs = new FileStream("d:\\data.dat", FileMode.Create))
                    {
                        fs.Write(bf, 0, bf.Length);
                        fs.Flush();
                        fs.Close();
                    }
                }
            }
        }

        static void FirstHandShake(Session session,int Offset,SessionData SesData)
        {
            if(Offset>0){
                if (session.ReadBuffer[0] == 0x03)
                {
                    SesData.ShakeData.Write(new byte[1] { 0x03 }, 0, 1);
                    SesData.ShakeData.Write(session.ReadBuffer, 1, Offset - 1);
                    SesData.ShakeData.Write(session.ReadBuffer, 1, Offset - 1);
                    SesData.Action = OperationAction.StartHandShake;
                    session.Connection.Write(SesData.ShakeData.ToArray());
                }
                else
                {
                    session.Close();
                }
            }
        }

        static void server_AppEnd(string SystemMessage)
        {
            //throw new NotImplementedException();
            Console.WriteLine("Server Stop :" + SystemMessage + " at:" + Thread.CurrentThread.ManagedThreadId);
        }

        static void server_AppError(string SystemMessage)
        {
            //throw new NotImplementedException();
            Console.WriteLine("Application Error :" + SystemMessage + " at:" + Thread.CurrentThread.ManagedThreadId);
        }

        static void server_AppStart(string SystemMessage)
        {
            //throw new NotImplementedException();
            Console.WriteLine("Server Start At Port 8888 at:" + Thread.CurrentThread.ManagedThreadId);
        }
    }
    class SessionData
    {
        public MemoryStream ShakeData
        {
            get;
            set;
        }

        public OperationAction Action
        {
            get;
            set;
        }
        public Queue<byte[]> ReadingQueue
        {
            get;
            set;
        }
        public SessionData()
        {
            ShakeData = new MemoryStream();
            ReadingQueue = new Queue<byte[]>();
            Action = OperationAction.Inited;
        }
    }
}
